package cn.ren.service;

import cn.ren.constant.MessageConstant;
import cn.ren.dao.MemberDao;
import cn.ren.dao.OrderDao;
import cn.ren.dao.OrderSettingDao;
import cn.ren.entity.Result;
import cn.ren.pojo.Member;
import cn.ren.pojo.Order;
import cn.ren.pojo.OrderSetting;
import cn.ren.util.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-03-01 11:24
 **/
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
        String orderDate = (String)map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        //1.判断这一天系统是否允许预约
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(date);
        if (orderSetting == null){
            //这一天没有预约计划
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.判断这一天是否已经预约满了
        int number = orderSetting.getNumber();      //这天可以预约的最大数量
        int reservations = orderSetting.getReservations();  //这天的已预约人数
        if (reservations >= number){
            //这一天预约已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //判断这个人是否是会员，不是自动注册（添加到数据库t_member）;
        //只要预约过，就必然是会员，所以非会员不会出现重复预约
        String telephone = (String)map.get("telephone");
        Member member = memberDao.findMemberByTel(telephone);
        if (member != null){
            //该手机号已经是会员了，判断是否是重复预约
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(
                    member.getId(),
                    date,
                    null,
                    null,
                    Integer.parseInt(setmealId));
            List<Order> list = orderDao.findOrderByCondition(order);
            if (list != null && list.size() > 0){
                //重复预约了
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        //可以添加预约，将这一天的预约人数+1
        orderSetting.setReservations(reservations + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        //非会员，自动注册到数据库
        if(member == null){
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //将该会员的预定保存到数据库
        Order order = new Order(member.getId(),
                date, (String) map.get("orderType"),
                Order.ORDERSTATUS_NO, Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById(id);
        if (map != null){
            //处理date，要变成字符串或者序列化，才能发送到页面
            Date orderDate = (Date)map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
